// pages/index/community_service/community_service.js
import Toast from "../../../miniprogram_npm/@vant/weapp/toast/toast"
import Dialog from "../../../miniprogram_npm/@vant/weapp/dialog/dialog"
const app = getApp()
const log = new app.appLogs()
const http = new app.appHttp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabIndex: 0,
    tabList: [{
      title: "全部",
      value: "5,6,7",
      isRefresh: false,
      pageNumber: 1,
      list: []
    },{
      title: "办事指南",
      value: "6",
      isRefresh: false,
      pageNumber: 1,
      list: []
    },{
      title: "政策法规",
      value: "5",
      isRefresh: false,
      pageNumber: 1,
      list: []
    },{
      title: "常见问题",
      value: "7",
      isRefresh: false,
      pageNumber: 1,
      list: []
    }
    ],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  /**
   * tab栏切换
   * @param e
   */
  onTabChange: function (e) {
    log.e(JSON.stringify(e.detail))
    this.setData({
      tabIndex: e.detail.index
    })
    if (this.data.tabList[e.detail.index].list.length <= 0) {
      this.getList()
    }
  },

  /**
   * 下拉刷新
   * @param e
   */
  onRefresh: function (e) {
    this.data.tabList[this.data.tabIndex].list = []
    this.data.tabList[this.data.tabIndex].isRefresh = true
    this.setData({
      tabList: this.data.tabList
    })
    this.getList()
  },

  /**
   * 上拉加载更多
   * @param e
   */
  onLower: function (e) {

  },

  /**
   * 获取数据
   * @param e
   */
  getList: function (e) {
    Toast.loading({
      duration: 0,
      message: "加载中..."
    })
    const tabIndex = this.data.tabIndex
    http.get("notice/list",{
      type_str: tabIndex == 0 ? this.data.tabList[0].value : "",
      type: tabIndex == 0 ? "" : this.data.tabList[tabIndex].value,
      applet_id: app.globalData.applet.applet_id,
      unit_code: app.globalData.unitCode,
    }).then(res => {
      Toast.clear()
      if (res.data.code === http.REQUEST_OK) {
        this.data.tabList[tabIndex].list = res.data.data
      }else {
        Toast.fail(res.data.msg)
      }
      this.data.tabList[tabIndex].isRefresh = false
      this.setData({
        tabList: this.data.tabList
      })
    }, err => {
      this.data.tabList[tabIndex].isRefresh = false
      this.setData({
        tabList: this.data.tabList
      })
      Toast.clear()
      Toast.fail("网络异常，请稍后再试！")
    })

  },


  /**
   * 详情界面
   * @param event
   */
  onDetails: function (event) {
    const id = event.currentTarget.dataset.id
    const content_id = event.currentTarget.dataset.content_id
    wx.navigateTo({
      url: '../notice/notice?id=' + id+"&content_id="+content_id
    })
  }

})